package com.huatai.user.generator;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * @program: nanming-saas
 * @description: 代码生成器
 * @author: songxiude
 * @create: 2023-12-08 14:24
 **/
public class MerchantGenerator {
		public static void main(String[] args) {
			List<String> tables = new ArrayList<>();
			tables.add("user_check_record");

//			String property = System.getProperty("user.dir");
//			System.out.println(property);

			FastAutoGenerator.create("jdbc:mysql://192.168.1.36:3306/ht_user_saas","root","datacenter@2023")
				.globalConfig(builder -> {
					builder.author("songxiude")               //作者
						.outputDir(System.getProperty("user.dir")+"\\ht-service\\ht-user\\src\\main\\java")    //输出路径(写到java目录)
						.enableSwagger()           //开启swagger
						.commentDate("yyyy-MM-dd")
						.fileOverride();            //开启覆盖之前生成的文件

				})
				.packageConfig(builder -> {
					builder.parent("com.huatai.user")
						//.moduleName("practice")
						.entity("entity")
						.service("service")
						.serviceImpl("service.impl")
						.controller("controller")
						.mapper("mapper")
						//.xml("mapper")
						.pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\ht-service\\ht-user\\src\\main\\resources\\mapper"));
				})
				.strategyConfig(builder -> {
					builder.addInclude(tables)
						//.addTablePrefix("p_")
						.serviceBuilder()
						.formatServiceFileName("%sService")
						.formatServiceImplFileName("%sServiceImpl")
						.entityBuilder()
						.enableLombok()
						.logicDeleteColumnName("deleted")
						.enableTableFieldAnnotation()
						.controllerBuilder()
						// 映射路径使用连字符格式，而不是驼峰
						.enableHyphenStyle()
						.formatFileName("%sController")
						.enableRestStyle()
						.mapperBuilder()
						//生成通用的resultMap
						.enableBaseResultMap()
						.superClass(BaseMapper.class)
						.formatMapperFileName("%sMapper")
						.enableMapperAnnotation()
						.formatXmlFileName("%sMapper");
				})
				.templateConfig(new Consumer<TemplateConfig.Builder>() {
					@Override
					public void accept(TemplateConfig.Builder builder) {
						// 实体类使用我们自定义模板
						builder.entity("templates/myentity.java");
					}
				})
				.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
				.execute();
		}
	}

